package com.education.teacher.redis;

import com.education.util.SerialUtil;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.Closeable;
import java.io.Serializable;
import java.util.*;


/**
 * Created by lzh on 2017/8/16.
 * 封装操作redis的方法，key value 都为byte[]
 */
public class RedisManager {

    @Autowired
    private JedisPool jedisPool;


    /**
     * 获取一个键的值
     * @param key
     * @return
     */
    public <T> T get(Serializable key) {

        try (Jedis jedis = jedisPool.getResource()){
            byte[] value = jedis.get(SerialUtil.Object2Bytes(key));
            return SerialUtil.bytes2Object(value);
        }
    }

    /**
     * 新建或者更新一个value
     * @param key
     * @param value
     */
    public void set(Serializable key, Object value) {
        try (Jedis jedis = jedisPool.getResource()){
            jedis.set(SerialUtil.Object2Bytes(key), SerialUtil.Object2Bytes(value));
        }

    }


    /**
     *  删除一个key
     * @param key
     */
    public void delete(Serializable key){
        try (Jedis jedis = jedisPool.getResource()){
            jedis.del(SerialUtil.Object2Bytes(key));
        }
    }

    /**
     * 根据给定前缀获取所有的value集合
     * @param prefix
     * @return 对象数组
     */
    public <T> List<T> getPrefixBy(String prefix){
        try (Jedis jedis = jedisPool.getResource()){
            Set<byte[]> values=jedis.keys(SerialUtil.Object2Bytes(prefix+"*"));//获取所有key的集合
            Iterator<byte[]> iterator=values.iterator();
            List<T> list=new ArrayList<>();
            while (iterator.hasNext()){
                T t=SerialUtil.bytes2Object(jedis.get(iterator.next()));//迭代取出所有value,并反序列为对象
                list.add(t);
            }
            jedis.disconnect();
            return list;
        }
    }


    /**
     *  删除指定前缀的所有键
     */
    public void deletePrefixBy(String prefix){

        try (Jedis jedis = jedisPool.getResource()){
            Set<byte[]> values=jedis.keys(SerialUtil.Object2Bytes(prefix+"*"));//获取所有key的集合
            Iterator<byte[]> iterator=values.iterator();
            while (iterator.hasNext()){
                jedis.del(iterator.next());
            }
        }
    }

    /**
     * 获取指定前缀key的数量
     * @param prefix
     * @return
     */
    public int getKeysNumByPrefix(String prefix){
        try (Jedis jedis = jedisPool.getResource()){
            int num=jedis.keys(SerialUtil.Object2Bytes(prefix+"*")).size();
            return num;
        }
    }

    public <K> Set<K> getKeys(String prefix){
        try (Jedis jedis = jedisPool.getResource()){
            Set<K> set=new HashSet<>();
            Set<byte[]> keys=jedis.keys(SerialUtil.Object2Bytes(prefix+"*"));
            Iterator<byte[]> iterator = keys.iterator();
            while (iterator.hasNext()){
                set.add(SerialUtil.bytes2Object(iterator.next()));
            }
            return set;
        }
    }

    public void setJedisPool(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }


}
